package com.bbva.tasks;


import com.microstrategy.utils.log.Level;
import com.microstrategy.web.app.tasks.BeanTaskRequestContext;
import com.microstrategy.web.app.tasks.ReportExecutionTask;
import com.microstrategy.web.beans.ReportBean;
import com.microstrategy.web.beans.TransformInstance;
import com.microstrategy.web.beans.WebComponent;
import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectsFactory;
import com.microstrategy.web.objects.WebReportInstance;
import com.microstrategy.web.objects.WebReportSource;
import com.microstrategy.web.tasks.TaskException;
import com.microstrategy.web.tasks.TaskRequestMalformedException;
import com.microstrategy.web.transform.StyleCatalog;
import com.microstrategy.web.transform.Transform;

public class CustReportGetHistoryTask extends ReportExecutionTask {

	private String sVersion="DHLM 1.9.multi 2013-01-28";

	public CustReportGetHistoryTask() {
		super();
		String FUNCTION_NAME = "CustomReportExecutionTask";
		Log.logger.logp(Level.INFO, CLASS_NAME, FUNCTION_NAME ,"["+sVersion+ "]Start Custom Report Execution");
	}
	
	protected void handleEvent(BeanTaskRequestContext beantaskrequestcontext)
	throws TaskException
    {
		String FUNCTION_NAME ="handleEvent";
		System.out.println("handleEvent");
		Log.logger.logp(Level.INFO, CLASS_NAME, FUNCTION_NAME  , " Custom Report history get Execution");
		 try
	     {
			 String msgId= beantaskrequestcontext.getRequestKeys().getValue("msgID");
			 String styleName=beantaskrequestcontext.getRequestKeys().getValue("styleName");
			 WebComponent webComponent = beantaskrequestcontext.getWebComponent();

	         WebIServerSession isess = beantaskrequestcontext.getWebIServerSession("sessionState", null);
	         WebObjectsFactory woFact = isess.getFactory();
	         WebReportSource reptSrc = woFact.getReportSource();
	         WebReportInstance rptInst = reptSrc.getInstance(msgId);
	         ReportBean rb = (ReportBean)beantaskrequestcontext.getWebBean();
	         rb.setReportInstance(rptInst);
	         webComponent.setBeanContext(rb.getBeanContext());
	         webComponent.setTargetBean(rb);
	         StyleCatalog styleCatalog = beantaskrequestcontext.getStyleCatalog();
	 		 Transform transform=styleCatalog.getTransformByStyle(styleName);
	 		 TransformInstance ti1 = rb.addTransform(transform);
	         ti1.setKey(styleName);
	         //beantaskrequestcontext.getWebBean().transform();
	         //webComponent.handleRequest(beantaskrequestcontext.getRequestKeys());
	      }
	     catch(Exception exception)
	     {
	    	 exception.printStackTrace();
	    	 throw new TaskRequestMalformedException((new StringBuilder()).append("The task '").append(getID()).append("' could not be completed due to an event handling error: ").append(exception.getMessage()).toString());
	     }
    }
	
	
	 private String CLASS_NAME = "com.bbva.tasks.CustomReportExecutionTask";
	 
}